//
//  DynamicIPViewController.m
//  Router
//
//  Created by gejiangs on 15/10/15.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "DynamicIPViewController.h"
#import "ChooseWIFITypeView.h"
#import "FixedIPViewController.h"
#import "BroadbandIPViewController.h"
#import "DiscoverHelper.h"
@interface DynamicIPViewController ()

@property (weak, nonatomic) IBOutlet UIButton *notButton;
@property (weak, nonatomic) IBOutlet UILabel *netLabel;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (nonatomic, strong) ChooseWIFITypeView *wifiTypeView;
@property (nonatomic, strong) DiscoverHelper *discoverHelper;
@end

@implementation DynamicIPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.discoverHelper  = [[DiscoverHelper alloc] init];
    self.title = @"动态IP设置";
    
    [self initUI];
}

-(void)initUI
{
    [self.netLabel addAttributesText:@"DHCP" color:COLOR_BASE];
    self.notButton.layer.borderColor = COLOR_BASE.CGColor;
    self.notButton.layer.borderWidth = 1.f;
    self.notButton.layer.cornerRadius = 5.f;
    self.okButton.layer.cornerRadius = 5.f;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (IBAction)notButtonClicked:(UIButton *)sender {
    __weak __typeof(self)weakSelf = self;
    [self.wifiTypeView setSelectedBlock:^(NSInteger index) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSMutableArray *vcs = [NSMutableArray arrayWithArray:strongSelf.navigationController.viewControllers];
        [vcs removeLastObject];
        BaseViewController *controller;
        if (index == 0)
        {
           controller = [[BroadbandIPViewController alloc] init];
        }
        else if (index == 1)
        {
            controller = [[FixedIPViewController alloc] init];
        }
        else{
            controller = [[DynamicIPViewController alloc] init];
        }
        [vcs addObject:controller];
        [strongSelf.navigationController setViewControllers:vcs animated:NO];
    }];
    [self.wifiTypeView showInView:self.view];
}

- (IBAction)okButtonClicked:(id)sender {
    __weak __typeof(self)weakSelf = self;
    [self.discoverHelper discoverWIFISuccess:^(BOOL succ, id responseObject, NSString *msg) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSLog(@"responseObject:%@",responseObject);
        NSString *info = responseObject[@"info"];
        if ([info isEqualToString:@"InterNetConnected"])
        {
//            NSString *type = responseObject[@"type"];
//            if ([type isEqualToString:@"dhcp"])
//            {
//                [strongSelf gotoWithType:NetTypeDHCP];
//            }
//            else if ([type isEqualToString:@"static"]){
//                [strongSelf gotoWithType:NetTypePPPOE];
//            }
//            else if ([type isEqualToString:@"ip"]){
//                [strongSelf gotoWithType:NetTypeIP];
//            }
            [strongSelf promptMessage:@"网络连接正常!"];
            [self dispatchTimerWithTime:2.0 block:^{
                [strongSelf.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            NSString *type = responseObject[@"type"];
            if ([type isEqualToString:@"linkoff"])
            {
                [self promptMessage:@"网线没有插入WAN口!"];
            }
        }
    } failure:^(NSError *error) {
        NSString *failedReason = [error localizedDescription];
        [self promptMessage:failedReason];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark -
#pragma mark   getters and setters
- (ChooseWIFITypeView *)wifiTypeView
{
    if (_wifiTypeView == nil)
    {
        _wifiTypeView = [ChooseWIFITypeView loadViewFromNib];
    }
    return _wifiTypeView;
}
@end
